<!--
 * @Date: 2021-12-14 10:43:56
 * @LastEditors: LeeKing
 * @LastEditTime: 2021-12-20 20:13:08
 * @FilePath: \admin\src\views\authority\roles\index.vue
-->
<template>
  <div class="RoleList">
    <!-- //角色列表 -->
    <el-card class="box-card">
      <el-button
        type="primary"
        style="margin: 0 0 20px 0"
        @click="UserDialog = true"
      >添加用户</el-button>
      <!-- //^表格 -->
      <el-table :data="rolesData" style="width: 100%" :border="true">
        <el-table-column type="expand">
          <template v-slot="scope">
            <el-row
              v-for="item in scope.row.children"
              :key="item.id"
              class="bdtop vcenter"
            >
              <el-col :span="8" style="background: red">
                <el-tag closable @close="removeTag(scope.row, item.id)">
                  {{ item.authName }}
                </el-tag>
                <i class="el-icon-caret-right" />
              </el-col>
              <el-col :span="16" style="background: green">
                <el-row
                  v-for="item1 in item.children"
                  :key="item1.id"
                  class="vcenter bdbottom"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeTag(scope.row, item1.id)"
                    >
                      {{ item1.authName }}
                    </el-tag>
                    <i class="el-icon-caret-right" />
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="item2 in item1.children"
                      :key="item2.id"
                      type="warning"
                      closable
                      @close="removeTag(scope.row, item2.id)"
                    >
                      {{ item2.authName }}
                    </el-tag>
                    <i class="el-icon-caret-right" />
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index" />
        <el-table-column label="角色名称" prop="roleName" />
        <el-table-column label="角色描述" prop="roleDesc" />
        <el-table-column label="操作" prop="desc">
          <template v-slot="scope">
            <!-- 修改按钮 -->
            <!--size 大小属性-->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editRoles(scope.row.id)"
            >编辑</el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delRoles(scope.row.id)"
            >删除</el-button>
            <!-- 分配角色按钮 -->
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="allotRoles(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加用户 编辑用户 -->

      <el-dialog
        :title="ruleForm.roleId ? '修改角色' : '添加角色'"
        :visible.sync="UserDialog"
        width="50%"
        @close="addDialogClose"
      >
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="ruleForm.roleName" />
          </el-form-item>
          <el-form-item label="角色描述" prop="roleDesc">
            <el-input v-model="ruleForm.roleDesc" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="UserDialog = false">取 消</el-button>
          <el-button
            type="primary"
            @click="editUserInfo(ruleForm)"
          >确 定</el-button>
        </span>
      </el-dialog>

      <!-- 分配权限 -->
      <el-dialog
        title="分配权限"
        :visible.sync="PowerDialogVisible"
        width="40%"
      >
        <el-tree
          :props="props"
          :load="loadNode"
          lazy
          show-checkbox
          @check-change="handleCheckChange"
        />
        <span slot="footer" class="dialog-footer">
          <el-button @click="PowerDialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="PowerDialogVisible = false"
          >确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  RolesList,
  RemoveRolePower,
  getRoleInfoById,
  editRoleInfo,
  addRole,
  delRole
} from '@/api/role.js'
export default {
  name: 'RoleList',
  data() {
    return {
      rolesData: [],
      UserDialog: false,
      ruleForm: {},
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      PowerDialogVisible: false,
      props: {
        label: 'name',
        children: 'zones'
      }
    }
  },

  created() {
    this.loadRoleList()
  },

  methods: {
    async loadRoleList() {
      const res = await RolesList()
      console.log(res)
      this.rolesData = res.data
    },

    // 删除

    //     :roleId	角色 ID
    // :rightId	权限 ID
    async removeTag(row, id) {
      // console.log(obj)
      // console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await RemoveRolePower(row.id, id)
          // console.log(res)
          this.$message.success('删除权限成功')
          console.log(res)
        })
        .catch(() => {})
    },
    // 编辑
    async editRoles(id) {
      this.UserDialog = true
      console.log(id)
      const res = await getRoleInfoById(id)
      console.log(res)
      this.ruleForm = res.data
    },
    // 编辑请求
    async editUserInfo(e) {
      if (e.roleId) {
        // 编辑用户
        await editRoleInfo(e)
        this.loadRoleList()
        this.$message.success('编辑成功！')
      } else {
        // 添加用户
        const res = await addRole(e)
        console.log(res)
        this.loadRoleList()
        this.$message.success('添加成功！')
      }
      this.UserDialog = false
    },
    // 重置
    addDialogClose() {
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        roleDesc: '',
        roleName: ''
      }
    },
    // 删除用户
    delRoles(e) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delRole(e)
          console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadRoleList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 分配角色
    allotRoles(e) {
      this.PowerDialogVisible = true
    }
  }
}
</script>

<style scoped lang="less"></style>
